﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.Model;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class PostDetail : System.Web.UI.Page
    {
        public PostModel PostModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
                PostService service = new PostService();
                PostModel = service.GetPostModel(Request.QueryString[Constants.QUERYSTR_POST_ID]);
            //}
        }

        protected void TaskFileGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
                ((HyperLink)e.Row.FindControl("ViewFileHyperLink")).NavigateUrl = string.Format("~/PostFile/{0}_{1}", ((PostFile)e.Row.DataItem).ID, ((PostFile)e.Row.DataItem).FileName);
            }
        }

        protected void ModalPopupExtender1_Load(object sender, EventArgs e)
        {
            PostService postService = new PostService();
            TaskFileGridView.DataSource = postService.GetPostFiles(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID])).ToList();
            TaskFileGridView.DataBind();
        }

        protected void uploadTaskFileBtn_Click(object sender, EventArgs e)
        {
            if (TaskFileFileUpload.HasFile)
            {
                PostService postService = new PostService();
                long fileID = postService.AddPostFile(TaskFileFileUpload.FileName, Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID]));
                TaskFileFileUpload.SaveAs(Server.MapPath(string.Format("PostFile/{0}_{1}", fileID, TaskFileFileUpload.FileName)));

                IQueryable<PostFile> files = postService.GetPostFiles(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID]));
                TaskFileGridView.DataSource = files.ToList();
                TaskFileGridView.DataBind();

                TaskFileModalPopupExtender.Show();
            }
        }

        protected void taskFileLnkBtn_Click(object sender, EventArgs e)
        {
            PostService postService = new PostService();
            TaskFileGridView.DataSource = postService.GetPostFiles(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_POST_ID])).ToList();
            TaskFileGridView.DataBind();

            TaskFileModalPopupExtender.Show();
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<PostFile> dds = new List<PostFile>();
            dds.Add(new PostFile());
            return dds;
        }

        protected void returnHyperLink_Click(object sender, EventArgs e)
        {
            if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.Customer)
            {
                Response.Redirect("~/CustomerPost.aspx");
            }
            else
            {
                Response.Redirect("~/SearchPost.aspx");
            }
        }
    }
}
